package com.bawei.module_goods.ui

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_goods.R
import com.bawei.module_goods.entity.DetailEntity
import com.bumptech.glide.Glide

// 新增：支持传入viewCount
class FootPrintAdapter(private val data: List<Pair<DetailEntity, Int?>>) : RecyclerView.Adapter<FootPrintAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ivGoodsImage: ImageView = view.findViewById(R.id.ivGoodsImage)
        val tvGoodsName: TextView = view.findViewById(R.id.tvGoodsName)
        val tvGoodsPrice: TextView = view.findViewById(R.id.tvGoodsPrice)
        val tvViewCount: TextView = view.findViewById(R.id.tvViewCount)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_foot_print, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val (item, viewCount) = data[position]
        holder.tvGoodsName.text = item.title ?: "未知商品"
        holder.tvGoodsPrice.text = "￥${item.purchasePrice ?: 0}"
        holder.tvViewCount.text = "浏览次数: ${viewCount ?: 0}"
        Glide.with(holder.ivGoodsImage.context)
            .load(item.mainImgUrl)
            .placeholder(R.mipmap.ic_launcher)
            .into(holder.ivGoodsImage)
        // 跳转到商品详情
        holder.itemView.setOnClickListener {
            val context = holder.itemView.context
            val intent = Intent(context, GoodsDetailActivity::class.java)
            intent.putExtra("goods_id", item.id ?: -1)
            context.startActivity(intent)
        }
    }

    override fun getItemCount() = data.size
} 